package cn.zjh.org.controller;

import cn.zjh.basic.annotation.PreAuthorize;
import cn.zjh.basic.util.AjaxResult;
import cn.zjh.basic.util.PageList;
import cn.zjh.org.domain.Department;
import cn.zjh.org.query.DepartmentQuery;
import cn.zjh.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "部门管理的api",description = "部门管理的crud")
@RequestMapping("/department")
@RestController
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    /**
     * 查询单个
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个")
    @PreAuthorize(name = "查询单个对象",sn = "department:getOne")
    public Department findOne(@PathVariable("id") Long id) {
        return departmentService.findOne(id);
    }

    /**
     * 查询所有
     * @return
     */
    @ApiOperation(value = "查询所有")
    @GetMapping
    @PreAuthorize(name = "查询所有对象",sn = "department:getAll")
    public List<Department> findAll() {
        return departmentService.findAll();

    }

    /**
     * 分页查询
     * @param departmentQuery
     * @return
     */
    @ApiOperation(value = "分页查询")
    @PostMapping
    @PreAuthorize(name = "部门列表",sn = "department:list")
    public PageList<Department> queryDat(@RequestBody DepartmentQuery departmentQuery){
        return departmentService.queryPage(departmentQuery);
    }
    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    @PreAuthorize(name = "部门删除",sn = "department:delete")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            departmentService.del(id);
            return new AjaxResult();//true + "操作成功"
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
     * 添加或修改
     * @param department
     * @return
     */
    @ApiOperation(value = "添加或修改")
    @PutMapping
    @PreAuthorize(name = "部门保存",sn = "department:save")
    public AjaxResult addOrUpdate(@RequestBody Department department){
        try {
            if(department.getId() == null){//添加
                departmentService.save(department);
            }else{
                departmentService.upDate(department);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
     * 批量删除
     * @param
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除")
    @PreAuthorize(name = "批量删除",sn = "department:patchDelete")
    public AjaxResult patchDelete(@RequestBody Long[] ids){
        try {
            departmentService.patchDelete(ids);
            return new AjaxResult();//true + "操作成功"
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"批量删除失败");
        }
    }

    //DepartmentController
    /**
     * 查询部门树
     * @return
     */
    @GetMapping("/deptTree")
    @ApiOperation(value = "获取部门树-无限极接口")
    public List<Department> deptTree(){
        return departmentService.deptTree();
    }

}
